package com.wanli.agent.homepage;

import android.os.Bundle;
import android.text.InputFilter;
import android.util.Log;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.Gson;
import com.wanli.agent.R;
import com.wanli.agent.base.BaseActivity;
import com.wanli.agent.base.DataCallBack;
import com.wanli.agent.bean.BaseResponseBean;
import com.wanli.agent.bean.RateNewBean;
import com.wanli.agent.homepage.model.HomePageModelImpl;
import com.wanli.agent.homepage.model.IHomePageModel;
import com.wanli.agent.utils.CashierInputFilterThree;
import com.wanli.agent.utils.ToastUtil;
import com.wanli.agent.widget.AppTitleBar;
import com.wanli.agent.widget.ViewLoading;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class CodeCardAwardActivity extends BaseActivity {

    @BindView(R.id.title_bar)
    AppTitleBar titleBar;
    @BindView(R.id.tv_big_award_name)
    TextView tvBigAwardName;
    @BindView(R.id.edit_big_award_value)
    EditText editBigAwardValue;
    @BindView(R.id.tv_small_award_name)
    TextView tvSmallAwardName;
    @BindView(R.id.edit_small_award_value)
    EditText editSmallAwardValue;


    @BindView(R.id.tv_small_money)
    TextView tv_small_money;
    @BindView(R.id.edit_small_money)
    EditText edit_small_money;

    @BindView(R.id.tv_unit_money)
    TextView tv_unit_money;
    @BindView(R.id.edit_unit_money)
    EditText edit_unit_money;

    @BindView(R.id.tv_firm_money)
    TextView tv_firm_money;
    @BindView(R.id.edit_firm_money)
    EditText edit_firm_money;



    @BindView(R.id.tv_commit)
    TextView tvCommit;
    private String agent_id;
    private IHomePageModel homePageModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_code_card_award);
        ButterKnife.bind(this);
        initView();
    }

    public void initView() {
        agent_id = getIntent().getStringExtra("agent_id");
        homePageModel = new HomePageModelImpl();
        requestShareMoneyRateNewMy();
        requestShareMoneyRateNew();

        InputFilter[] filters = {new CashierInputFilterThree()};
        editBigAwardValue.setFilters(filters);
        editSmallAwardValue.setFilters(filters);


    }

    public void requestShareMoneyRateNewMy() {
        homePageModel.requestShareMoneyRateNewMy(new DataCallBack<RateNewBean>() {
            @Override
            public void onSuccessful(RateNewBean bean) {
                setRateNewMy(bean);
            }

            @Override
            public void onFailed(String msg, String code) {

            }
        });
    }

    public void requestShareMoneyRateNew() {
        homePageModel.requestShareMoneyRateNew(agent_id, new DataCallBack<RateNewBean>() {
            @Override
            public void onSuccessful(RateNewBean bean) {
                setRateNew(bean);
            }

            @Override
            public void onFailed(String msg, String code) {

            }
        });
    }

    /**
     * 'waiver' => 1,         //免手续费
     * 'wechat_alipay' => 2,  //微信支付宝
     * 'wechat_alipay_d0' => 8, //d0费用
     * <p>
     * 'quickpass_debit' => 3,  //借记卡优惠费率
     * 'quickpass_credit' => 4, //贷记卡(信用卡)优惠费率
     * <p>
     * 'debit_card' => 5,      //借记卡标准费率
     * 'debit_card_cap' => 6,  //借记卡标准费率封顶
     * 'credit_card' => 7 ,     //贷记卡(信用卡)标准费率
     * <p>
     * 'card_bonus'    =>  9,  //码牌奖励费率(80)
     * 'card_bonus_low'=>  10,  //码牌奖励费率
     */
    public void setRateNewMy(RateNewBean bean) {
        List<RateNewBean.DataBean> mList = bean.getData();
        for (RateNewBean.DataBean dataBean : mList) {
            switch (dataBean.getRule_type()) {
                case 9:
                    tvBigAwardName.setText("大奖励(参考" + dataBean.getRate() + ")");
                    break;
                case 10:
                    tvSmallAwardName.setText("小奖励(参考" + dataBean.getRate() + ")");
                    break;
                case 14:
                    tv_small_money.setText("小微返现(参考" + dataBean.getRate() + ")");
                    break;
                case 15:
                    tv_unit_money.setText("个体返现(参考" + dataBean.getRate() + ")");
                    break;
                case 16:
                    tv_firm_money.setText("企业返现(参考" + dataBean.getRate() + ")");
                    break;

            }
        }
    }

    public void setRateNew(RateNewBean bean) {
        List<RateNewBean.DataBean> mList = bean.getData();
        for (RateNewBean.DataBean dataBean : mList) {
            switch (dataBean.getRule_type()) {
                case 9:
                    editBigAwardValue.setText(dataBean.getRate());
                    editBigAwardValue.setSelection(dataBean.getRate().length());
                    break;
                case 10:
                    editSmallAwardValue.setText(dataBean.getRate());
                    editSmallAwardValue.setSelection(dataBean.getRate().length());
                    break;
                case 14:
                    edit_small_money.setText(dataBean.getRate());
                    edit_small_money.setSelection(dataBean.getRate().length());
                    break;
                case 15:
                    edit_unit_money.setText(dataBean.getRate());
                    edit_unit_money.setSelection(dataBean.getRate().length());
                    break;
                case 16:
                    edit_firm_money.setText(dataBean.getRate());
                    edit_firm_money.setSelection(dataBean.getRate().length());
                    break;
            }
        }
    }

    public void setShareMoneyRateNew() {
        List<RateNewBean.DataBean> mList = new ArrayList<>();
        for (int i = 9; i < 14; i++) {
            RateNewBean.DataBean dataBean = new RateNewBean.DataBean();
            dataBean.setRule_type(i);
            switch (i) {
                case 9:
                    dataBean.setRate(editBigAwardValue.getText().toString());
                    break;
                case 10:
                    dataBean.setRate(editSmallAwardValue.getText().toString());
                    break;
                case 11:
                    dataBean.setRule_type(14);
                    dataBean.setRate(edit_small_money.getText().toString());
                    break;
                case 12:
                    dataBean.setRule_type(15);
                    dataBean.setRate(edit_unit_money.getText().toString());
                    break;
                case 13:
                    dataBean.setRule_type(16);
                    dataBean.setRate(edit_firm_money.getText().toString());
                    break;
            }
            mList.add(dataBean);
        }
        Log.e("dddddd=",new Gson().toJson(mList));
        ViewLoading.showProgress(mActivity, "提交中......");
        homePageModel.requestSetShareMoneyRateNew(agent_id, mList, new DataCallBack<BaseResponseBean>() {
            @Override
            public void onSuccessful(BaseResponseBean bean) {
                ToastUtil.showShort(bean.getMsg());
                finish();
            }

            @Override
            public void onFailed(String msg, String code) {

            }
        });
    }

    @OnClick(R.id.tv_commit)
    public void onViewClicked() {
        setShareMoneyRateNew();
    }
}
